﻿using System;
using System.Data;
using System.Text;
using System.Data.OleDb;
using Maticsoft.DBUtility;//请先添加引用
namespace BookSystem.DAL
{
    /// <summary>
    /// 数据访问类Book。
    /// </summary>
    public class Book
    {
        public Book()
        { }
        #region  成员方法


        /// <summary>
        /// 增加一条数据
        /// 修改：去掉UUID
        /// </summary>
        public void Add(BookSystem.Model.Book model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Book(");
            strSql.Append("purchasecount,content,publisher,class_uuid,storefile,booknumber,bookname,author,book_language,isbn,purchasetime,publishtime,price)");
            strSql.Append(" values (");
            strSql.Append("@purchasecount,@content,@publisher,@class_uuid,@storefile,@booknumber,@bookname,@author,@book_language,@isbn,@purchasetime,@publishtime,@price)");
            OleDbParameter[] parameters = {
                    //new OleDbParameter("@uuid", OleDbType.Integer,4),
					new OleDbParameter("@purchasecount", OleDbType.Integer,4),
					new OleDbParameter("@content", OleDbType.VarChar,255),
					new OleDbParameter("@publisher", OleDbType.VarChar,255),
					new OleDbParameter("@class_uuid", OleDbType.VarChar,255),
					new OleDbParameter("@storefile", OleDbType.VarChar,255),
					new OleDbParameter("@booknumber", OleDbType.VarChar,255),
					new OleDbParameter("@bookname", OleDbType.VarChar,255),
					new OleDbParameter("@author", OleDbType.VarChar,255),
					new OleDbParameter("@book_language", OleDbType.VarChar,255),
					new OleDbParameter("@isbn", OleDbType.VarChar,255),
					new OleDbParameter("@purchasetime", OleDbType.Date),
					new OleDbParameter("@publishtime", OleDbType.Date),
					new OleDbParameter("@price", OleDbType.Double,8)};
            //parameters[0].Value = model.uuid;
            parameters[0].Value = model.purchasecount;
            parameters[1].Value = model.content;
            parameters[2].Value = model.publisher;
            parameters[3].Value = model.class_uuid;
            parameters[4].Value = model.storefile;
            parameters[5].Value = model.booknumber;
            parameters[6].Value = model.bookname;
            parameters[7].Value = model.author;
            parameters[8].Value = model.book_language;
            parameters[9].Value = model.isbn;
            parameters[10].Value = model.purchasetime;
            parameters[11].Value = model.publishtime;
            parameters[12].Value = model.price;

            DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(BookSystem.Model.Book model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Book set ");
            strSql.Append("purchasecount=" + model.purchasecount + ",");
            strSql.Append("content='" + model.content + "',");
            strSql.Append("publisher='" + model.publisher + "',");
            strSql.Append("class_uuid='" + model.class_uuid + "',");
            strSql.Append("storefile='" + model.storefile + "',");
            strSql.Append("booknumber='" + model.booknumber + "',");
            strSql.Append("bookname='" + model.bookname + "',");
            strSql.Append("author='" + model.author + "',");
            strSql.Append("book_language='" + model.book_language + "',");
            strSql.Append("isbn='" + model.isbn + "',");
            strSql.Append("purchasetime='" + model.purchasetime + "',");
            strSql.Append("publishtime='" + model.publishtime + "',");
            strSql.Append("price=" + model.price + "");
            strSql.Append(" where uuid=" + model.uuid + " ");
            DbHelperOleDb.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BookSystem.Model.Book GetModel(int uuid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select uuid,purchasecount,content,publisher,class_uuid,storefile,booknumber,bookname,author,language,isbn,purchasetime,publishtime,price from Book ");
            strSql.Append(" where uuid=@uuid ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@uuid", OleDbType.Integer,4)};
            parameters[0].Value = uuid;

            BookSystem.Model.Book model = new BookSystem.Model.Book();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["uuid"].ToString() != "")
                {
                    model.uuid = int.Parse(ds.Tables[0].Rows[0]["uuid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["purchasecount"].ToString() != "")
                {
                    model.purchasecount = int.Parse(ds.Tables[0].Rows[0]["purchasecount"].ToString());
                }
                model.content = ds.Tables[0].Rows[0]["content"].ToString();
                model.publisher = ds.Tables[0].Rows[0]["publisher"].ToString();
                model.class_uuid = ds.Tables[0].Rows[0]["class_uuid"].ToString();
                model.storefile = ds.Tables[0].Rows[0]["storefile"].ToString();
                model.booknumber = ds.Tables[0].Rows[0]["booknumber"].ToString();
                model.bookname = ds.Tables[0].Rows[0]["bookname"].ToString();
                model.author = ds.Tables[0].Rows[0]["author"].ToString();
                model.book_language = ds.Tables[0].Rows[0]["book_language"].ToString();
                model.isbn = ds.Tables[0].Rows[0]["isbn"].ToString();
                if (ds.Tables[0].Rows[0]["purchasetime"].ToString() != "")
                {
                    model.purchasetime = DateTime.Parse(ds.Tables[0].Rows[0]["purchasetime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["publishtime"].ToString() != "")
                {
                    model.publishtime = DateTime.Parse(ds.Tables[0].Rows[0]["publishtime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["price"].ToString() != "")
                {
                    model.price = double.Parse(ds.Tables[0].Rows[0]["price"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select uuid,purchasecount,content,publisher,class_uuid,storefile,booknumber,bookname,author,book_language,isbn,purchasetime,publishtime,price ");
            strSql.Append(" FROM Book ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            OleDbParameter[] parameters = {
                    new OleDbParameter("@tblName", OleDbType.VarChar, 255),
                    new OleDbParameter("@fldName", OleDbType.VarChar, 255),
                    new OleDbParameter("@PageSize", OleDbType.Integer),
                    new OleDbParameter("@PageIndex", OleDbType.Integer),
                    new OleDbParameter("@IsReCount", OleDbType.Bit),
                    new OleDbParameter("@OrderType", OleDbType.Bit),
                    new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
                    };
            parameters[0].Value = "Book";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}

